package com.project.class3_31;

import javax.swing.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端
 */
public class Client {
    public Client(){
        try {
            //创建socket对象，连接指定IP和端口的服务器
            Socket socket= new Socket("127.0.0.1",8088);
            //创建读取流
            InputStream in= socket.getInputStream();
            //创建写入流
            OutputStream out=socket.getOutputStream();

            String str= JOptionPane.showInputDialog(null,"请输入发送数据");
            //向服务器发送数据
            out.write(str.getBytes());
            out.flush();

            byte [] byteArray= new byte[1024];
            in.read(byteArray);
            String s = new String(byteArray).trim();
            System.out.println("服务器发送:"+s);

            //关闭流
            out.close();
            in.close();
            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        new Client();
    }
}
